<?php

namespace App\Console\Commands\Assess;

use App\Models\Assess\ActivateUserManagementEmailModel;
use App\Models\Register\ActivateUserManagementModel;
use App\Services\Assess\ActivateService;
use App\Services\Assess\SendLogService;
use App\Services\Common\Ding;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class EmailSendCrontab extends Command
{
    /**
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'email_send
                            {--status=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '激活买家管理,手动设置定时 发送邮件信息';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $ActivateUserManagementEmailModel = new ActivateUserManagementEmailModel();
        $ActivateService = new ActivateService();
        $ActivateUserManagementModel = new ActivateUserManagementModel();
        $Ding = new Ding();
        $offset = 0;
        $limit = 40;
        while (true) {
            try {
                $list = $ActivateUserManagementEmailModel->where('status', '=', 0)
                    ->offset($offset)
                    ->limit($limit)
                    ->get()->toArray();

                $offset += $limit;
                if (empty($list)) break;

                foreach ($list as $val) {

                    //判断是否到达发送时间
                    $dateTime = date('Y-m-d H:i',strtotime($val['date_time']));
                    if (date('Y-m-d H:i') < $dateTime){
                        continue;
                    }

                    $activateData = $ActivateUserManagementModel->getOne([
                        ['id', '=', $val['activate_user_id']],
                    ], 'order_id,account,mark');
                    $redisKey = 'email:activate:' . $val['id'];

                    if (empty($activateData) || $activateData['mark'] == 0) {
                        $status = 2;
                    } else {

                        //判断redis中是否有数据
                        if (Redis::get($redisKey)) {
                            continue;
                        }
                        $emailData = [
                            'id' => $val['activate_user_id'],
                            'title' => $val['title'],
                            'content' => $val['content'],
                            'send_email_type' => SendLogService::SEND_EMAIL_TYPE_SYSTEM
                        ];
                        $response = $ActivateService->emailService($emailData);
                        $status = 1;
                        if ($response['code'] == 1) {
                            $status = 2;
                            //发送钉钉
                            $msg = '订单：' . $activateData['order_id'] . '对应账号：' . $activateData['account'] . ($response['message']?? '');
                            $Ding->text($msg);
                        }
                    }

                    $ActivateUserManagementEmailModel->where('id', '=', $val['id'])->update(['status' => $status]);
                    //存储并设置过期时间
                    Redis::setex($redisKey, 5, 1);
                    Log::channel('emailLog')->debug('management_email激活买家管理id:'.$val['activate_user_id'].'定时发送邮件成功:',['params' => $emailData]);
                }

            } catch (\Exception $e) {
                $Ding->text($e->getMessage() . ' ' . $e->getFile() . ' ' . $e->getLine());
            }

        }
    }
}



